﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace DeepTalkClientWebServer
{

    /// <summary>
    /// Uses the Vista/W7 interfaces to find out who is the correct web browser to run
    /// (i.e. the user's web browser).
    /// </summary>
    class FindProperWebBrowser
    {
        /// <summary>
        /// Returns the default program to open the "http" protocal.
        /// The start info only has the FIleName and Arguments set.
        /// Everything else is inited to ProcessStartInfo defaults.
        /// </summary>
        /// <returns></returns>
        public static ProcessStartInfo DefaultBrowserInfo()
        {
            ///
            /// Use the sytem to get the browser info
            /// 

            IApplicationAssociationRegistration reg =
                (IApplicationAssociationRegistration)new ApplicationAssociationRegistration();

            string result;
            reg.QueryCurrentDefault("http",
                ASSOCIATIONTYPE.AT_URLPROTOCOL,
                ASSOCIATIONLEVEL.AL_EFFECTIVE,
                out result);

            ///
            /// Parse out the command name and the arguments. The command name is surrounded by """'s usually.
            /// 

            int cmdEnd = -1;
            if (result[0] == '"')
            {
                cmdEnd = result.IndexOf('"', 2);
                if (cmdEnd > 0)
                {
                    cmdEnd = cmdEnd + 1;
                }
            }
            else
            {
                cmdEnd = result.IndexOf(' ');
            }

            if (cmdEnd < 0)
            {
                cmdEnd = result.Length;
            }

            int argStart = cmdEnd + 1;

            string program = result.Substring(0, cmdEnd);
            string arguments = "";
            if (argStart < result.Length)
            {
                arguments = result.Substring(argStart);
            }

            ///
            /// Now, parse out the command line arguments, etc., so we can build up the correct start info.
            /// 

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = program;
            info.Arguments = arguments;

            return info;
        }

    }

    public enum ASSOCIATIONLEVEL
    {
        AL_MACHINE,
        AL_EFFECTIVE,
        AL_USER,
    };

    public enum ASSOCIATIONTYPE
    {
        AT_FILEEXTENSION,
        AT_URLPROTOCOL,
        AT_STARTMENUCLIENT,
        AT_MIMETYPE,
    };

    [Guid("4e530b0a-e611-4c77-a3ac-9031d022281b"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IApplicationAssociationRegistration
    {
        void QueryCurrentDefault([In, MarshalAs(UnmanagedType.LPWStr)] string pszQuery,
        [In, MarshalAs(UnmanagedType.I4)] ASSOCIATIONTYPE atQueryType,
        [In, MarshalAs(UnmanagedType.I4)] ASSOCIATIONLEVEL alQueryLevel,
        [Out, MarshalAs(UnmanagedType.LPWStr)] out string ppszAssociation);

        void QueryAppIsDefault(
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszQuery,
            [In] ASSOCIATIONTYPE atQueryType,
            [In] ASSOCIATIONLEVEL alQueryLevel,
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszAppRegistryName,
            [Out] out bool pfDefault);

        void QueryAppIsDefaultAll(
            [In] ASSOCIATIONLEVEL alQueryLevel,
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszAppRegistryName,
            [Out] out bool pfDefault);

        void SetAppAsDefault(
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszAppRegistryName,
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszSet,
            [In] ASSOCIATIONTYPE atSetType);

        void SetAppAsDefaultAll(
            [In, MarshalAs(UnmanagedType.LPWStr)] string pszAppRegistryName);

        void ClearUserAssociations();
    }

    [ComImport, Guid("591209c7-767b-42b2-9fba-44ee4615f2c7")]//
    class ApplicationAssociationRegistration
    {
    }

}
